<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- qqmlcontext.cpp -->
  <title>QQmlContext  类 | Qt QML 5.12.3</title>
  <link rel="stylesheet" type="text/css" href="style/offline-simple.css" />
  <script type="text/javascript">
    document.getElementsByTagName("link").item(0).setAttribute("href", "style/offline.css");
    // loading style sheet breaks anchors that were jumped to before
    // so force jumping to anchor again
    setTimeout(function() {
        var anchor = location.hash;
        // need to jump to different anchor first (e.g. none)
        location.hash = "#";
        setTimeout(function() {
            location.hash = anchor;
        }, 0);
    }, 0);
  </script>
</head>
<body>
<div class="header" id="qtdocheader">
  <div class="main">
    <div class="main-rounded">
      <div class="navigationbar">
        <table><tr>
<td ><a href="../qtdoc/index.html">Qt 5.12</a></td><td ><a href="qtqml-index.html">Qt QML</a></td><td ><a href="qtqml-module.html">C++ 类</a></td><td >QQmlContext</td></tr></table><table class="buildversion"><tr>
<td id="buildversion" width="100%" align="right"><a href="qtqml-index.html">Qt 5.12.3 参考指南</a></td>
        </tr></table>
      </div>
    </div>
<div class="content">
<div class="line">
<div class="content mainContent">
<div class="sidebar">
<div class="toc">
<h3><a name="toc">目录</a></h3>
<ul>
<li class="level1"><a href="#public-types">公用类型</a></li>
<li class="level1"><a href="#public-functions">公用函数</a></li>
<li class="level1"><a href="#static-public-members">静态公用成员</a></li>
<li class="level1"><a href="#details">详细描述</a></li>
<li class="level2"><a href="#the-context-hierarchy">上下文层次结构</a></li>
</ul>
</div>
<div class="sidebar-content" id="sidebar-content"></div></div>
<h1 class="title">QQmlContext 类</h1>
<!-- $$$QQmlContext-brief -->
<p><a href="qqmlcontext.html">QQmlContext</a>类定义QML引擎中的上下文。<a href="#details">更多…</a></p>
<!-- @@@QQmlContext -->
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> 头文件：</td><td class="memItemRight bottomAlign">   <span class="preprocessor">#include &lt;QQmlContext&gt;</span>
</td></tr><tr><td class="memItemLeft rightAlign topAlign"> qmake:</td><td class="memItemRight bottomAlign"> QT += qml</td></tr><tr><td class="memItemLeft rightAlign topAlign">继承自：</td><td class="memItemRight bottomAlign"> <a href="../qtcore/qobject.html">QObject</a></td></tr></table></div><ul>
<li><a href="qqmlcontext-members.html"></a></li>
<li><a href="qqmlcontext-obsolete.html">废弃的成员</a></li>
</ul>
<a name="public-types"></a>
<h2 id="public-types">公用类型</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> class </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext-propertypair.html">PropertyPair</a></b></td></tr>
</table></div>
<a name="public-functions"></a>
<h2 id="public-functions">公用函数</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></b>(QQmlEngine *<i>engine</i>, QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#QQmlContext-1">QQmlContext</a></b>(QQmlContext *<i>parentContext</i>, QObject *<i>parent</i> = nullptr)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> virtual </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#dtor.QQmlContext">~QQmlContext</a></b>() override</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QUrl </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#baseUrl">baseUrl</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QObject *</td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#contextObject">contextObject</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QVariant </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#contextProperty">contextProperty</a></b>(const QString &amp;<i>name</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QQmlEngine *</td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#engine">engine</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> bool </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#isValid">isValid</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QString </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#nameForObject">nameForObject</a></b>(QObject *<i>object</i>) const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QQmlContext *</td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#parentContext">parentContext</a></b>() const</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> QUrl </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#resolvedUrl">resolvedUrl</a></b>(const QUrl &amp;<i>src</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setBaseUrl">setBaseUrl</a></b>(const QUrl &amp;<i>baseUrl</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextObject">setContextObject</a></b>(QObject *<i>object</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextProperties">setContextProperties</a></b>(const QVector&lt;QQmlContext::PropertyPair&gt; &amp;<i>properties</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextProperty">setContextProperty</a></b>(const QString &amp;<i>name</i>, QObject *<i>value</i>)</td></tr>
<tr><td class="memItemLeft rightAlign topAlign"> void </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#setContextProperty-1">setContextProperty</a></b>(const QString &amp;<i>name</i>, const QVariant &amp;<i>value</i>)</td></tr>
</table></div>
<ul>
<li class="fn">31 个公用函数继承自<a href="../qtcore/qobject.html#public-functions">QObject</a></li>
</ul>
<a name="static-public-members"></a>
<h2 id="static-public-members">静态公用成员</h2>
<div class="table"><table class="alignedsummary">
<tr><td class="memItemLeft rightAlign topAlign"> const QMetaObject </td><td class="memItemRight bottomAlign"><b><a href="qqmlcontext.html#staticMetaObject-var">staticMetaObject</a></b></td></tr>
</table></div>
<ul>
<li class="fn">9 个静态公用成员继承自<a href="../qtcore/qobject.html#static-public-members">QObject</a></li>
</ul>
<h3>额外的继承成员</h3>
<ul>
<li class="fn">1 个属性继承自<a href="../qtcore/qobject.html#properties">QObject</a></li>
<li class="fn">1 个公用槽函数继承自<a href="../qtcore/qobject.html#public-slots">QObject</a></li>
<li class="fn">2 个信号继承自<a href="../qtcore/qobject.html#signals">QObject</a></li>
<li class="fn">9 个保护函数继承自<a href="../qtcore/qobject.html#protected-functions">QObject</a></li>
</ul>
<a name="details"></a>
<!-- $$$QQmlContext-description -->
<div class="descr">
<h2 id="details">详细描述</h2>
<p><a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>类定义QML引擎中的上下文。</p>
<p>上下文允许向 QML 引擎实例化的 QML 组件公开数据。</p>
<p>每个<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext 都</a>包含一组与其<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qobject.html">QObject</a>属性不同的属性，这些属性允许通过名称将数据显式绑定到上下文。上下文属性通过调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#setContextProperty">QQmlContext::setContextProperty</a> ()来定义和更新。下面的示例显示了一个 Qt 模型被绑定到一个上下文，然后从 QML 文件访问。</p>
<pre class="cpp">

  <span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> engine;
  <span class="type"><a href="../qtcore/qstringlistmodel.html">QStringListModel</a></span> modelData;
  <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());
  context<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;myModel&quot;</span><span class="operator">,</span> <span class="operator">&amp;</span>modelData);

  <span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&amp;</span>engine);
  component<span class="operator">.</span>setData(<span class="string">&quot;import QtQuick 2.0\nListView { model: myModel }&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>());
  <span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> <span class="operator">*</span>window <span class="operator">=</span> component<span class="operator">.</span>create(context);

</pre>
<p>请注意，删除它构造的任何<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>是创建者的责任。如果在销毁组件实例<code>context</code>时不再需要示例中的对象<code>window</code>，则<code>context</code>必须显式销毁该对象。确保这一点的最简单方法是设置<code>window</code>为<code>context<span lang="zh-cn">。</span></code></p>
<p>为了简化绑定和维护更大的数据集，可以在<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>上设置上下文对象。上下文对象的所有属性都可以在上下文中通过名称使用，就好像它们都是通过调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#setContextProperty">QQmlContext::setContextProperty</a> ()单独添加的。通过属性的通知信号检测对属性值的更改。设置上下文对象比手动添加和维护上下文属性值更快、更容易。</p>
<p>下面的示例与前一个示例具有相同的效果，但它使用了上下文对象。</p>
<pre class="cpp">

  <span class="keyword">class</span> MyDataSet : <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span> {
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
      Q_PROPERTY(<span class="type"><a href="../qtcore/qabstractitemmodel.html">QAbstractItemModel</a></span> <span class="operator">*</span>myModel READ model NOTIFY modelChanged)
      <span class="operator">.</span><span class="operator">.</span><span class="operator">.</span>
  };

  MyDataSet myDataSet;
  <span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> engine;
  <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());
  context<span class="operator">-</span><span class="operator">&gt;</span>setContextObject(<span class="operator">&amp;</span>myDataSet);

  <span class="type"><a href="qqmlcomponent.html">QQmlComponent</a></span> component(<span class="operator">&amp;</span>engine);
  component<span class="operator">.</span>setData(<span class="string">&quot;import QtQuick 2.0\nListView { model: myModel }&quot;</span><span class="operator">,</span> <span class="type"><a href="../qtcore/qurl.html">QUrl</a></span>());
  component<span class="operator">.</span>create(context);

</pre>
<p>由<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#setContextProperty">QQmlContext::setContextProperty</a> ()显式添加的所有属性优先于上下文对象的属性。</p>
<a name="the-context-hierarchy"></a>
<h4 >上下文层次结构</h4>
<p>上下文形成层次结构。这个层次结构的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html#rootContext">根</a>是 QML 引擎的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html#rootContext">根上下文</a>。子上下文继承其父上下文的属性；如果子上下文设置了其父上下文中已存在的上下文属性，则新上下文属性将覆盖父上下文的属性。</p>
<p>以下示例定义了两个上下文 ——<code>context1</code>和<code>context2</code>。第二个上下文用新值覆盖从第一个上下文继承的“b”上下文属性。</p>
<pre class="cpp">

  <span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> engine;
  <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context1 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(engine<span class="operator">.</span>rootContext());
  <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> <span class="operator">*</span>context2 <span class="operator">=</span> <span class="keyword">new</span> <span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span>(context1);

  context1<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;a&quot;</span><span class="operator">,</span> <span class="number">12</span>);
  context1<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;b&quot;</span><span class="operator">,</span> <span class="number">12</span>);

  context2<span class="operator">-</span><span class="operator">&gt;</span>setContextProperty(<span class="string">&quot;b&quot;</span><span class="operator">,</span> <span class="number">15</span>);

</pre>
<p>虽然在上下文中实例化的QML对象严格地不属于该上下文中，但它们的绑定属于该上下文中。如果一个上下文被销毁，未执行的QML对象的属性绑定将停止计算。</p>
<p><b>警告：</b> 在该上下文中创建对象后设置上下文对象或添加新的上下文属性是一项昂贵的操作（实质上是强制重新评估所有绑定）。因此，只要有可能，您应该在使用上下文创建任何对象之前完成上下文的“设置”。</p>
</div>
<p><b>请参考 </b>
<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qtqml-cppintegration-exposecppattributes.html">
将 C++ 类型的属性暴露给 QML</a><span lang="zh-cn">。</span></p>
<!-- @@@QQmlContext -->
<div class="func">
<h2>成员函数文档</h2>
<!-- $$$QQmlContext[overload1]$$$QQmlContextQQmlEngine*QObject* -->
<h3 class="fn" id="QQmlContext"><a name="QQmlContext"></a>QQmlContext::<span class="name">QQmlContext</span>(<span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> *<i>engine</i>, <span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>创建一个新的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>作为<i>engine</i>根上下文的子级和<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qobject.html">QObject</a>  <i>parent</i>。</p>
<!-- @@@QQmlContext -->
<!-- $$$QQmlContext$$$QQmlContextQQmlContext*QObject* -->
<h3 class="fn" id="QQmlContext-1"><a name="QQmlContext-1"></a>QQmlContext::<span class="name">QQmlContext</span>(<span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> *<i>parentContext</i>, <span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>parent</i> = nullptr)</h3>
<p>使用给定的<i>parentContext</i>和<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qobject.html">QObject </a><i>parent</i>创建一个新的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>。 </p>
<!-- @@@QQmlContext -->
<!-- $$$~QQmlContext[overload1]$$$~QQmlContext -->
<h3 class="fn" id="dtor.QQmlContext"><a name="dtor.QQmlContext"></a><code>[override virtual] </code>QQmlContext::<span class="name">~QQmlContext</span>()</h3>
<p>销毁<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>。</p>
<p>任何依赖于此上下文的表达式或子上下文都将失效，但不会被销毁（除非它们是<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>对象的父级）。</p>
<!-- @@@~QQmlContext -->
<!-- $$$baseUrl[overload1]$$$baseUrl -->
<h3 class="fn" id="baseUrl"><a name="baseUrl"></a><span class="type"><a href="../qtcore/qurl.html">QUrl</a></span> QQmlContext::<span class="name">baseUrl</span>() const</h3>
<p>返回组件的基本 url，如果没有设置，则返回包含的组件。</p>
<p><b>请参考 </b><a href="qqmlcontext.html#setBaseUrl">setBaseUrl</a>()<span lang="zh-cn">。</span></p>
<!-- @@@baseUrl -->
<!-- $$$contextObject[overload1]$$$contextObject -->
<h3 class="fn" id="contextObject"><a name="contextObject"></a><span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *QQmlContext::<span class="name">contextObject</span>() const</h3>
<p>返回上下文对象，如果没有上下文对象，则返回 0。</p>
<p><b>请参考 </b><a href="qqmlcontext.html#setContextObject">setContextObject</a>()<span lang="zh-cn">。</span></p>
<!-- @@@contextObject -->
<!-- $$$contextProperty[overload1]$$$contextPropertyconstQString& -->
<h3 class="fn" id="contextProperty"><a name="contextProperty"></a><span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> QQmlContext::<span class="name">contextProperty</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>name</i>) const</h3>
<p>R将此上下文的<i>name</i>属性值作为<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtcore/qvariant.html">QVariant </a>
<span lang="zh-cn">返回。</span></p>
<p><b>请参考 </b><a href="qqmlcontext.html#setContextProperty">setContextProperty</a>()<span lang="zh-cn">。</span></p>
<!-- @@@contextProperty -->
<!-- $$$engine[overload1]$$$engine -->
<h3 class="fn" id="engine"><a name="engine"></a><span class="type"><a href="qqmlengine.html">QQmlEngine</a></span> *QQmlContext::<span class="name">engine</span>() const</h3>
<p>返回上下文的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html">QQmlEngine</a>，如果上下文没有<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html">QQmlEngine</a>或<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlengine.html">QQmlEngine</a>被破坏，则返回0 。</p>
<!-- @@@engine -->
<!-- $$$isValid[overload1]$$$isValid -->
<h3 class="fn" id="isValid"><a name="isValid"></a><span class="type">bool</span> QQmlContext::<span class="name">isValid</span>() const</h3>
<p>返回上下文是否有效。</p>
<p>一个上下文要有效，必须有引擎，而且它的<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#contextObject">contextObject</a> ()，如果有的话，一定不能被删除。</p>
<!-- @@@isValid -->
<!-- $$$nameForObject[overload1]$$$nameForObjectQObject* -->
<h3 class="fn" id="nameForObject"><a name="nameForObject"></a><span class="type"><a href="../qtcore/qstring.html">QString</a></span> QQmlContext::<span class="name">nameForObject</span>(<span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>object</i>) const</h3>
<p>返回此上下文中<i>object</i> 的名称，如果<i>object</i> 未在上下文中命名，则返回空字符串。对象由<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#setContextProperty">setContextProperty</a> ()命名，或者在 QML 创建上下文的情况下由 ids命名。</p>
<p>如果对象有多个名称，则返回第一个。</p>
<!-- @@@nameForObject -->
<!-- $$$parentContext[overload1]$$$parentContext -->
<h3 class="fn" id="parentContext"><a name="parentContext"></a><span class="type"><a href="qqmlcontext.html#QQmlContext">QQmlContext</a></span> *QQmlContext::<span class="name">parentContext</span>() const</h3>
<p>返回上下文的父级<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">QQmlContext</a>，如果此上下文没有父级或父级已被销毁，则返回0。</p>
<!-- @@@parentContext -->
<!-- $$$resolvedUrl[overload1]$$$resolvedUrlconstQUrl& -->
<h3 class="fn" id="resolvedUrl"><a name="resolvedUrl"></a><span class="type"><a href="../qtcore/qurl.html">QUrl</a></span> QQmlContext::<span class="name">resolvedUrl</span>(const <span class="type"><a href="../qtcore/qurl.html">QUrl</a></span> &amp;<i>src</i>)</h3>
<p>解析URL<span lang="zh-cn"> </span><i>src</i>相对于包含组件的URL。</p>
<p><b>请参考 </b><a href="qqmlengine.html#baseUrl">QQmlEngine::baseUrl</a>()
<span lang="zh-cn">和 </span><a href="qqmlcontext.html#setBaseUrl">setBaseUrl</a>()<span lang="zh-cn">。</span></p>
<!-- @@@resolvedUrl -->
<!-- $$$setBaseUrl[overload1]$$$setBaseUrlconstQUrl& -->
<h3 class="fn" id="setBaseUrl"><a name="setBaseUrl"></a><span class="type">void</span> QQmlContext::<span class="name">setBaseUrl</span>(const <span class="type"><a href="../qtcore/qurl.html">QUrl</a></span> &amp;<i>baseUrl</i>)</h3>
<p>显式设置 url<span lang="zh-cn">， </span>
<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#resolvedUrl">
resolveUrl</a> () 将用于对<i>baseUrl 的</i>相对引用。</p>
<p>调用此函数将覆盖默认使用的包含组件的 url。</p>
<p><b>请参考 </b><a href="qqmlcontext.html#baseUrl">baseUrl</a>()
<span lang="zh-cn">和 </span><a href="qqmlcontext.html#resolvedUrl">resolvedUrl</a>()<span lang="zh-cn">。</span></p>
<!-- @@@setBaseUrl -->
<!-- $$$setContextObject[overload1]$$$setContextObjectQObject* -->
<h3 class="fn" id="setContextObject"><a name="setContextObject"></a><span class="type">void</span> QQmlContext::<span class="name">setContextObject</span>(<span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>object</i>)</h3>
<p>设置上下文<i>object</i>。</p>
<p><b>请参考 </b><a href="qqmlcontext.html#contextObject">contextObject</a>()<span lang="zh-cn">。</span></p>
<!-- @@@setContextObject -->
<!-- $$$setContextProperties[overload1]$$$setContextPropertiesconstQVector<QQmlContext::PropertyPair>& -->
<h3 class="fn" id="setContextProperties"><a name="setContextProperties"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperties</span>(const <span class="type"><a href="../qtcore/qvector.html">QVector</a></span>&lt;<span class="type"><a href="qqmlcontext-propertypair.html">QQmlContext::PropertyPair</a></span>&gt; &amp;<i>properties</i>)</h3>
<p>在此上下文中设置一批<i>properties</i> 。</p>
<p>一次性设置所有属性可以避免不必要的刷新表达式，因此建议不要为每个单独的属性调用<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html#setContextProperty">setContextProperty</a> ()。</p>
<p>这个函数在Qt 5.11中引入。</p>
<p><b>请参考 </b><a href="qqmlcontext.html#setContextProperty">QQmlContext::setContextProperty</a>()<span lang="zh-cn">。</span></p>
<!-- @@@setContextProperties -->
<!-- $$$setContextProperty[overload1]$$$setContextPropertyconstQString&QObject* -->
<h3 class="fn" id="setContextProperty"><a name="setContextProperty"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperty</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>name</i>, <span class="type"><a href="../qtcore/qobject.html#QObject">QObject</a></span> *<i>value</i>)</h3>
<p>在此上下文中设置<i>name</i> 属性的<i>value</i> 。</p>
<p>
<a href="file:///E:/WLL/work/Qt5.12-Document-Trans-in-Chinese/qtqml/qqmlcontext.html">
QQmlContext</a>并<b>没有</b>取得其所有权的<i>value</i>。</p>
<p><b>请参考 </b><a href="qqmlcontext.html#contextProperty">contextProperty</a>()<span lang="zh-cn">。</span></p>
<!-- @@@setContextProperty -->
<!-- $$$setContextProperty$$$setContextPropertyconstQString&constQVariant& -->
<h3 class="fn" id="setContextProperty-1"><a name="setContextProperty-1"></a><span class="type">void</span> QQmlContext::<span class="name">setContextProperty</span>(const <span class="type"><a href="../qtcore/qstring.html">QString</a></span> &amp;<i>name</i>, const <span class="type"><a href="../qtcore/qvariant.html">QVariant</a></span> &amp;<i>value</i>)</h3>
<p>置该<i>value</i> 的<i>名字</i>在这个上下文属性。</p>
<!-- @@@setContextProperty -->
</div>
        </div>
       </div>
   </div>
   </div>
</div>
<div class="footer">
   <p>
   <acronym title="Copyright">&copy;</acronym> 2019 The Qt Company Ltd.
   Documentation contributions included herein are the copyrights of
   their respective owners.<br/>    The documentation provided herein is licensed under the terms of the    <a href="http://www.gnu.org/licenses/fdl.html">GNU Free Documentation    License version 1.3</a> as published by the Free Software Foundation.<br/>    Qt and respective logos are trademarks of The Qt Company Ltd.     in Finland and/or other countries worldwide. All other trademarks are property
   of their respective owners. </p>
</div>
</body>
</html>
